/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.servlet.mvc.condition;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Supports media type expressions as described in:
 * {@link RequestMapping#consumes()} and {@link RequestMapping#produces()}.
 *
 * @author Arjen Poutsma
 * @author Rossen Stoyanchev
 * @since 3.1
 */
abstract class AbstractMediaTypeExpression implements MediaTypeExpression, Comparable<AbstractMediaTypeExpression> {

	protected final Log logger = LogFactory.getLog(getClass());

	/**本表达式表示的媒体类型
	 */
	private final MediaType mediaType;

	/**表达式是否是反向匹配
	 */
	private final boolean isNegated;


	AbstractMediaTypeExpression(String expression) {
		// 如果表达式字符串以！开头，则为反向匹配
		if (expression.startsWith("!")) {
			// isNegated标记为反向匹配
			this.isNegated = true;
			// 去掉！
			expression = expression.substring(1);
		}
		else {
			// 反向匹配为false
			this.isNegated = false;
		}
		// 表达式中字符串解析为媒体类型，赋给表达式的mediaType属性
		this.mediaType = MediaType.parseMediaType(expression);
	}

	/**直接指定属性值的构造方法
	 * @param mediaType
	 * @param negated
	 */
	AbstractMediaTypeExpression(MediaType mediaType, boolean negated) {
		this.mediaType = mediaType;
		this.isNegated = negated;
	}


	@Override
	public MediaType getMediaType() {
		return this.mediaType;
	}

	@Override
	public boolean isNegated() {
		return this.isNegated;
	}


	@Override
	// 表达式比较，对媒体类型表达式排序时使用
	public int compareTo(AbstractMediaTypeExpression other) {
		// 使用媒体类型特殊比较器进行比较，具体的排序逻辑根据媒体类型的特殊性进行排序。
		// 媒体类型特殊性越高、排序时越靠前。特殊性是根据是否有通配符来决定。
		// 如“*/*”，“text/*”、“text/html”这三个表达式的排序（由低到高）为*/* < text/* < text/html，但在媒体类型的父类型或子类不同时，无法进行比较，视为相等
		// 在父类型和子类型均相同时，可以根据参数中的q质量因子来排序，如text/html;q=0.9 > text/html;q=0.1，
		// 无参数时q默认为1，q相同时，按照参数数量比较，多的特殊性高，排序靠前
		return MediaType.SPECIFICITY_COMPARATOR.compare(this.getMediaType(), other.getMediaType());
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj != null && getClass() == obj.getClass()) {
			AbstractMediaTypeExpression other = (AbstractMediaTypeExpression) obj;
			return (this.mediaType.equals(other.mediaType) && this.isNegated == other.isNegated);
		}
		return false;
	}

	@Override
	public int hashCode() {
		return this.mediaType.hashCode();
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		if (this.isNegated) {
			builder.append('!');
		}
		builder.append(this.mediaType.toString());
		return builder.toString();
	}

}
